Skip to content

Refactor VMCollection#445

Open
hippalectryon-0 wants to merge 8 commits intoQubesOS:mainfrom
hippalectryon-0:refactor-vmcollection
Open

Refactor VMCollection#445
hippalectryon-0 wants to merge 8 commits intoQubesOS:mainfrom
hippalectryon-0:refactor-vmcollection

Conversation

@hippalectryon-0
Copy link

@hippalectryon-0 hippalectryon-0 commented Mar 10, 2026

This PR is a rewrite of VMCollection.
Requires #438

Goals:

  • Clearer code and docstrings
  • No functional change

Main changes:

Improved docstring

VMCollection is actually a fairly counter-intuitive object. It's not a true collection (as a dict or set or Mapping) since it holds two different collections - one for the vm.List cache and one containing blind objects - but depending on the method called one or the other may be used.

The new docstring aims to fairly convey that.

Better variable names

Previous variables names did not properly convey what the variables did.

Rewrite of the vm_objects / vm_dict split

The new code uses vms / known_names instead, which 1) is clearer 2) allows for simpler logic in e.g. get_blind()

Minor changes:

Split and renamed refresh_cache

The old refresh_cache by default did not refresh the cache (force=False). It has been split into _ensure_cache_loaded (=force=False) and refresh_cache (=force=True).

More direct vm access in __iter__, values

Since we already call _ensure_cache_loaded at the start of the method, we can use get_blind to avoid self._vms[name] > __getitem__ > __contains __ > get_blind

Remove app.cache_enabled logic

The VMCollection should not have to interact with app.cache_enabled, this should be internal to app. In other words, even if we pass a power_state= in __init__, if cache_enabled=False, then the app object should not used the passed value.
Looking at QubeBase's code, this is already the case: _power_state_cache is only not-None if cache_enabled=True - so in practice this should not change any behavior, only cleanup the code.

Note (see test test_101_list_selected): this "fixes" the fact that calling qvm-ls on a selected subset of VMs calls admin.vm.CurrentState even though this data is already returned by admin.vm.List. If I understand correctly this behavior was due to the fact that when ArgParser fetches a subset of VMs, it creates a new app object which by default has cache disabled, before the calling method can itself enable the cache.

@codecov
Copy link

codecov bot commented Mar 10, 2026

Codecov Report

❌ Patch coverage is 97.41379% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.39%. Comparing base (1a623d1) to head (c7365b7).

Files with missing lines Patch % Lines
qubesadmin/utils.py 70.00% 3 Missing ⚠️
qubesadmin/app.py 98.87% 1 Missing ⚠️
qubesadmin/base.py 98.14% 1 Missing ⚠️
qubesadmin/events/__init__.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #445      +/-   ##
==========================================
+ Coverage   76.24%   76.39%   +0.14%     
==========================================
  Files          53       53              
  Lines        9322     9362      +40     
==========================================
+ Hits         7108     7152      +44     
+ Misses       2214     2210       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant